package com.watson.onebox.jvm;

import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

/**
 * <p>
 * JMH 基准测试入门
 *
 * @author niujinpeng
 * @Date 2020/8/21 1:13
 */
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class JmhHello {
    public static String src = "put(ITEM_PV, new TableUtil.HeaderItem(ITEM_PV, \"商详PV\", TableUtil.Format.QFW, true, \"所选维度下，权限对应商品详情页产生的流量。\", true));\n" +
            "        put(ITEM_UV, new TableUtil.HeaderItem(ITEM_UV, \"商详UV\", TableUtil.Format.QFW, true, \"所选维度下，权限对应商品详情页产生的流量的访客数，按照浏览器唯一标示进行去重。除了微信平台，其他平台间汇总访客数直接累加。\", true));\n" +
            "        put(DEAL_ORD_UV_RATE, new TableUtil.HeaderItem(DEAL_ORD_UV_RATE, \"商详UV订单转化率-成交\", TableUtil.Format.PERCENT, true, \"成交子单数/商详UV。\", true));\n" +
            "        put(DEAL_AMT, new TableUtil.HeaderItem(DEAL_AMT, \"成交金额\", TableUtil.Format.JE, true, \"所选维度下，权限对应商品成交订单的成交金额。\", true));\n" +
            "        put(DEAL_ORD_QTTY, new TableUtil.HeaderItem(DEAL_ORD_QTTY, \"成交子单量\", TableUtil.Format.QFW, true, \"所选维度下，权限对应商品成交订单的子单量。\", true));\n" +
            "        put(DEAL_USER_NUM, new TableUtil.HeaderItem(DEAL_USER_NUM, \"成交客户数\", TableUtil.Format.QFW, true, \"在线以订单支付时间统计，货到以下单时间统计，统计周期内，支付（或货到）未取消的去重用户数。\", true));\n" +
            "        put(ITEM_LOGIN_PINS, new TableUtil.HeaderItem(ITEM_LOGIN_PINS, \"浏览用户数\", TableUtil.Format.QFW, true, \"所选维度下，权限对应商品详情页产生的流量的用户，按照用户账号进行去重，不同平台之前累加，账号去重，同时当账号为空时，统一记为1.\", true));\n" +
            "        put(PURCHASE_NUM, new TableUtil.HeaderItem(PURCHASE_NUM, \"加购人数\", TableUtil.Format.QFW, true, \"所选维度下，权限对应商品加购人数，当实时加购用户对商品进行下单，或取消加购时，加购人数相应减少，离线状态下，取商品当天最终加购状态。多天加购人数为累加后整体去重，类目主品牌部门等均为整体去重。由于加购数据目前无法区分用户加购时使用的设备和平台，所以目前只有“全部平台”下有加购数据，选择其他子平台，加购数据为0。\", true));\n" +
            "        put(PV_AVG, new TableUtil.HeaderItem(PV_AVG, \"人均PV\", TableUtil.Format.FLOAT_2, true, \"商详PV/商详UV\", true));\n" +
            "        put(RT_AVG, new TableUtil.HeaderItem(RT_AVG, \"平均停留时长\", TableUtil.Format.FLOAT_2, true, \"流量整体停留时长/商详页PV。\", true));\n" +
            "        put(DEAL_ORD_PV_RATE, new TableUtil.HeaderItem(DEAL_ORD_PV_RATE, \"商详PV订单转化率-成交\", TableUtil.Format.PERCENT, true, \"成交子单数/商详PV。\", true));\n" +
            "        put(UV_VALUE, new TableUtil.HeaderItem(UV_VALUE, \"UV价值\", TableUtil.Format.QFW, true, \"成交金额/商详UV\", true));\n" +
            "        put(PV_VALUE, new TableUtil.HeaderItem(PV_VALUE, \"商详PV价值\", TableUtil.Format.QFW, true, \"成交金额/商详PV。\", true));\n" +
            "        put(DEAL_RATE, new TableUtil.HeaderItem(DEAL_RATE, \"UV用户转化率-成交\", TableUtil.Format.PERCENT, true, \"成交客户数/商详UV\", true));\n" +
            "        put(DEAL_PV_RATE, new TableUtil.HeaderItem(DEAL_PV_RATE, \"商详PV用户转化率-成交\", TableUtil.Format.PERCENT, true, \"成交客户数/商详PV。\", true));";

    String string = "";
    StringBuilder stringBuilder = new StringBuilder();

    @Benchmark
    public String stringAdd() {
        for (int i = 0; i < 1000; i++) {
            string = string + src;
        }
        return string;
    }

    @Benchmark
    public String stringBuilderAppend() {
        for (int i = 0; i < 1000; i++) {
            stringBuilder.append(src);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(JmhHello.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
}
